package com.web.service.impl;

import com.web.dao.HotelDao;
import com.web.dao.OrderDao;
import com.web.dao.RoomDao;
import com.web.domain.*;
import com.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by 陈国钰 on 2016/9/7.
 */
@Service
@Transactional(rollbackFor = {java.lang.Exception.class})
public class OrderServiceImpl implements OrderService {
    /**
     * 空闲
     */
    public static final int IDLE = 0;
    /**
     * 出租
     */
    private static final int RENTAL = 1;

    /**
     * 订单
     */
    private static final int CANCLE_ORDER = 2;
    private static final int HAVED_PAY = 4;
    private static final int WAIT_CANCLE = 1;

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private HotelDao hotelDao;

    public void booking(Order order) {
        String[] roomList = order.getOrderContext().split(",");
        order.setOrderContext("");
        Hotel hotel = hotelDao.findHotelById(order.getHotelId());
        order.setHotelName(hotel.getHotelName());
        for (String s : roomList) {
            Room room = roomDao.searchRoomById(Integer.parseInt(s));
            room.setRoomStatus(RENTAL);
            OrderRoom orderRoom = new OrderRoom();
            orderRoom.setRoomId(Integer.parseInt(s));
            orderRoom.setRoomName(room.getRoomName());
            order.getorderRoomList().add(orderRoom);
            order.setOrderContext(order.getOrderContext() + "房间名:" + room.getRoomName() + " ");

            roomDao.modify(room);
        }
        order.setCreateTime(new Date());
        orderDao.modify(order);
    }

    private void modifyRoom(Room room) {
        room.setRoomStatus(RENTAL);
        roomDao.modify(room);
    }


    public List orderList(User user) {
        return orderDao.orderList(user);
    }


    public Order findOrder(int orderId) {
        return orderDao.findOrder(orderId);
    }


    public void pay(int orderId) {
        Order order = findOrder(orderId);
        order.setState(HAVED_PAY);
        orderDao.modify(order);
    }


    public void delete(int orderId) {
        Order order = findOrder(orderId);
        orderDao.delete(order);
    }


    public void cancleOrder(int orderId) {
        Order order = findOrder(orderId);
        order.setState(CANCLE_ORDER);
        orderDao.modify(order);
    }


    public void canclePayOrder(int orderId) {
        Order order = findOrder(orderId);
        order.setState(WAIT_CANCLE);
        orderDao.modify(order);
    }


    public void comment(Order order) {
        orderDao.modify(order);
    }
}
